package com.meshnetics.gb.type.impl.custom;

import com.meshnetics.gb.type.DataValue;

/**
 * List of values. Values only of selected type (specified in type of list) can be hold in list. Same or equal
 * values can be hold in list.
 *
 * @author AKovylin
 */
public abstract class ListDataValue extends CustomDataValue {
    private final DataValue[] elements;

    // no checks performed
    protected ListDataValue(ListDataType listType, DataValue[] elements) {
        super(listType);
        if (elements == null) {
            throw new NullPointerException("elements is null");
        }
        for (int i = 0; i < elements.length; i++) {
            if (elements[i] == null) {
                throw new NullPointerException("elements[" + i + "] is null");
            }
            if (!listType.getElementType().equals(elements[i].getType())) {
                throw new IllegalArgumentException("elements[" + i + "] type doesn't match list element type");
            }
        }
        this.elements = elements;
    }

    public int getListSize() {
        return elements.length;
    }

    public DataValue getElement(int index) {
        return elements[index];
    }
}
